package org.example.Tomcat.Util;

import org.example.JVM.ByteCode.Handler.BaseByteCodeHandler;
import org.example.JVM.ByteCode.Type.ClassFile;
import org.example.Tomcat.Handler.BaseRequestHandler;
import org.example.Tomcat.Handler.MethodHandler;
import org.example.Tomcat.Handler.ProtocolHandler;
import org.example.Tomcat.Handler.UrlHandler;
import org.example.Tomcat.Request;

import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @description:
 * @author: farprt
 * @create: 2023-02-13 16:58
 **/
public class RequestAnalysiser {

    public static final List<BaseRequestHandler> handlers=new ArrayList<>();

    static {
        handlers.add(new MethodHandler());
        handlers.add(new UrlHandler());
        handlers.add(new ProtocolHandler());
        handlers.sort(Comparator.comparingInt(BaseRequestHandler::order));
    }

    public static Request analysis(ByteBuffer byteBuffer) throws Exception {
        //重置ByteBuffer读指针，从头开始
        byteBuffer.position(0);
        Request request=new Request();
        //遍历解析器，调用每个解析器的解析方法
        for (BaseRequestHandler handler : handlers) {
            handler.read(byteBuffer, request);
        }
        return request;
    }


}